/*
 * The MIT License (MIT)
 *
 * File: uart.h
 *
 * Description: Uart library for msp430.
 *
 * Copyright (c) 2013 Ed Crampton
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "uart.h"
#include <string.h>
#include "../board.h"
#include <legacymsp430.h>

static UartTxBuffer_s mTxBuffer; //Static memeber
static UartRxBuffer_s mRxBuffer; //Static member
volatile uint8_t gRxEmpty; //Global variable User to detect if rx buffer has data

//Function definitions
//Name: BufferFull
//Description: Checks to see if our buffer is full
//Args:
//Returns:
static inline int BufferFull(uint8_t head,uint8_t tail,int bufferSize);

//helper functions
static inline int BufferFull(uint8_t head,uint8_t tail,int bufferSize)
{
   uint8_t temp = head - tail;
   if(temp == bufferSize + 1 )
   {
      return 1;//rturn 1 if buffer full
   }
   return 0;
}

//---------------------------------------------
//Function: UartInitialize()
//---------------------------------------------
void UartInitialize()
{
   //Set up Uart for 9600 Baud
   UCA0CTL1 |= UCSSEL_2;                     // SMCLK
   UCA0BR0 = 104;                            // 1MHz 9600
   UCA0BR1 = 0;                              // 1MHz 9600
   UCA0MCTL = UCBRS0;                        // Modulation UCBRSx = 1
   UCA0CTL1 &= ~UCSWRST;                     // Initialize USCI state machine
   IE2 |= UCA0RXIE;                          // Enable USCI_A0 RX interrupt

   //clear buffers
   memset(&mTxBuffer,0,sizeof(mTxBuffer));
   memset(&mRxBuffer,0,sizeof(mRxBuffer));
}

//------------------------------------------------------------------------------
//Function: UartTxByte()
//------------------------------------------------------------------------------
int UartTxByte(uint8_t byte)
{
   //check if buffer is full
   if(BufferFull(mTxBuffer.mHeadPtr,mTxBuffer.mTailPtr,TX_BUFF_SIZE) == 1)
   {
      return -1;
   }
   //Put byte onto buffer and ensure tx interrupt is enabled
   mTxBuffer.mData[mTxBuffer.mHeadPtr++%TX_BUFF_SIZE] = byte;
   IE2 |= UCA0TXIE;

   return 0;
}

void UartPuts(const char *byte)
{
   int i = 0;
   int ret_status;
   while(byte[i] != 0)
   {
      ret_status = UartTxByte(byte[i]);

      if(ret_status == 0)
      {
         ++i;
      }
   }
   return;
}

//---------------------------------------------
//ISR: USCI0TX_ISR()
//---------------------------------------------
interrupt (USCIAB0TX_VECTOR) USCI0TX_ISR(void)
{
   while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?

   if(mTxBuffer.mHeadPtr == mTxBuffer.mTailPtr)
   {
      IE2 &= ~UCA0TXIE;              // All bytes TXed, disable interrupt
      P1OUT &= ~GREEN_LED_PIN;  //turn off led
   }
   else
   {
      P1OUT ^= GREEN_LED_PIN;
      UCA0TXBUF = mTxBuffer.mData[mTxBuffer.mTailPtr++%TX_BUFF_SIZE];                    // TX -> RXed character
   }

}

//---------------------------------------------
//ISR: USCI0RX_ISR()
//---------------------------------------------
interrupt (USCIAB0RX_VECTOR) USCI0RX_ISR(void)
{
  mRxBuffer.mData[mRxBuffer.mHeadPtr++] = UCA0RXBUF;                    // TX -> RXed character
}
